<?php

if (isset($htmlcnpj)){    
    echo "<center>". utf8_encode($htmlcnpj)."</center>";    
}else{

define('COOKIELOCAL', str_replace('\\', '/', realpath('../')) . '/' . 'cookies_cnpj/');
@session_start();

$cookieFile = COOKIELOCAL . session_id();

// cria arquivo onde será salva a sessão com a receita
if (!file_exists($cookieFile)) {
    $file = fopen($cookieFile, 'w');
    fclose($file);
}

$url = 'http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/captcha/gerarCaptcha.asp';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
//curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

// não utilizar returntransfer , este script replica imagem captcha da receita
$imgsource = curl_exec($ch);
curl_close($ch);

$saveto = '../images/image.jpg';

if (file_exists($saveto)) {
    unlink($saveto);
}

$fp = fopen($saveto, 'x');
fwrite($fp, $imgsource);
fclose($fp);


?>

<html>
    <head>
        <title>CNPJ e Captcha</title>
    </head>
    <body>
            <form name="form" id="form" method="post" action="<?php echo base_url('index.php/empresas/processa') ?>">
                  <div class="row">
                    <div class="large-8 columns" style="left: 181px;">
                        <h1>
                            <small><i class="fi-magnifying-glass" style="font-size: 35px; padding-right: 10px; margin-left: 5px;"></i><span style="vertical-align: 6px;">Consulta de CNPJ</span></small>
                        </h1>
                    </div>                
                </div>
                <?php
                        $objCaptcha = new Captcha_Model();                        
                ?>
                <div class="row">                                 
                    <div class="small-4 columns" style="left: 181px">
                        <label>Informe o CNPJ:                        
                        <input type="text" name="CNPJ" maxlength="19" required />                         
                        </label>
                    </div>
                    <div class="small-4 columns" style="right: 152px">
                        <img hidden="true" name="imgcaptcha" id="imgcaptcha" src="../../../images/image.jpg" border="0">
                        <label>
                            Texto do Captcha:
                        </label>
                        <input type="text" name="CAPTCHA" maxlength="6" value="<?php $objCaptcha->submeterCaptcha(); ?>" required  />  
                    </div>           
                </div>
                <div class="row">
                    <div class="small-4 columns" style="left: 181px">
                        <input id="id_submit" name="enviar" type="submit" class="button success small radius" value="Consultar CNPJ"/>
                    </div>  
                </div>
            </form>        
</html>
<?php } ?>